#include "Act.h"
#include <QtGui>


Act::Act()
{

}

Act::Act(Bridget *bd)
{
    setWeight(bd->getWeight());

}

void Act::setName(QString name1)
{
    name=name1;
}

void Act::setTime(double time1)
{
    time=time1;
}

QString Act::getName()
{
    return name;
}
double Act::getTime()
{
    return time;
}

void Act::setWeight(double weight1)
{
    weight=weight1;
}

double Act::getWeight()
{
    return weight;
}

bool Act::inBase()
{
    bool t = false;
    QFile file("baseAct.xml");
    if (file.open(QIODevice::ReadOnly))
    {
        if (docAct.setContent(&file))
        {
            QDomElement root = docAct.documentElement();
            QDomElement child = root.firstChildElement("act");

            while ((!child.isNull()) && (child.attribute("name", "")!=name))
            {
                child = child.nextSiblingElement("act");
            }
            if (child.attribute("name", "")== name)
            {
                t=true;
            }
        }
        file.close();
    }

    return t;

}

bool Act::setPar()
{
    bool t=true;
    QFile file("baseAct.xml");
    if (file.open(QIODevice::ReadOnly))
    {
        if (docAct.setContent(&file))
        {
            QDomElement root = docAct.documentElement();
            QDomElement child = root.firstChildElement("act");
            qDebug()<<(child.attribute("name", ""));
            while ((!child.isNull()) && (child.attribute("name", "")!=name))
            {
                child = child.nextSiblingElement("act");

            }
            if (child.attribute("name", "")== name)
            {
                energy = child.firstChildElement("energy").text().toDouble()*weight*getTime()/60;
                qDebug()<<getTime();
                qDebug()<<weight;
                qDebug()<<child.firstChildElement("energy").text().toDouble();
                qDebug()<<"Energy: "<<energy;

            }
            else
            {
                t=false;
                //qDebug()<<"Not founded!";
                }
}
           else
            {

               qDebug()<<"No setContent!";
            }
            file.close();
            }

    return t;
}


double Act::getEnergy()
{
    return energy;
}



void Act::setEnergy(double energy1)
{
    energy=energy1;
}



void Act::addAct()
{
    QFile file("baseAct.xml");
    QFile file1 ("baseA.xml");
    if (file.open(QIODevice::ReadWrite))
    {
        if (docAct.setContent(&file))
        {
            qDebug()<<"In added!";

            const int Indent = 4;
            QDomElement root = docAct.documentElement();
            QDomElement act = docAct.createElement("act");
            act.setAttribute("name",name);
            QDomElement Energy = docAct.createElement("energy");

            QDomText energyText = docAct.createTextNode(QString::number(energy));

            root.appendChild(act);
            act.appendChild(Energy);
            Energy.appendChild(energyText);

            if (file1.open(QIODevice::ReadWrite))
            {
            QTextStream out(&file1);
            docAct.save(out, Indent);
        }
        }
        file.close();
        file1.close();
        file.remove();
        file1.rename("baseAct.xml");

    }
    
}

void Act::doRecordAct()
{
    QDate date;
    QDomDocument docJournal;

    QFile file("Journal.xml");
    QFile file1 ("tempJournal.xml");
    if (file.open(QIODevice::ReadWrite))
    {
        if (docJournal.setContent(&file))
        {


            const int Indent = 4;
            QDomElement root = docJournal.documentElement();
            QDomElement action = docJournal.createElement("act");
            action.setAttribute("date",date.currentDate().toString());
            QDomElement energy = docJournal.createElement("energy");
            QDomText energyText = docJournal.createTextNode(QString::number(getEnergy()));

            root.appendChild(action);
            action.appendChild(energy);
            energy.appendChild(energyText);

            if (file1.open(QIODevice::ReadWrite))
            {
            QTextStream out(&file1);
            docJournal.save(out, Indent);
            qDebug()<<"File1";
        }
        }
        file.close();
        file1.close();
        file.remove();
        file1.rename("Journal.xml");

    }
}

